/* octopus_out.c
 *
 * Output a (stream of) byte(s) in bitbang mode to the 
 * octopus245 chip that is (hopefully) attached. 
 *
 * We have a little board that has a FT245BM chip and 
 * the 8 outputs are connected to several different 
 * things that we can turn on and off with this program. 
 *
 * If you have an idea about hardware that can easily
 * interface onto an FTDI chip, I'd like to collect
 * ideas. If I find it worthwhile to make, I'll consider
 * making it, I'll even send you a prototype (against
 * cost-of-material) if you want.
 * 
 * At "harddisk-recovery.nl" they have a little board that 
 * controls the power to two harddrives and two fans. 
 * 
 * -- REW R.E.Wolff@BitWizard.nl
 *
 *
 *
 * This program was based on liboctopus_example_bitbang2232.c 
 * which doesn't carry an author or attribution header.
 *
 *
 * This program is distributed under the GPL, version 2. 
 * Millions copies of the GPL float around the internet.
 */


#include <stdio.h>
#include <unistd.h>
#include <octopus.h>

void octopus_fatal (struct octopus_context *octopus, char *str)
{
#if 0
    fprintf (stderr, "%s: %s\n",
             str, octopus_get_error_string (octopus));
    exit (1);
#endif
}

int main(int argc, char **argv)
{
#if 0
    struct octopus_context octopusc;
    int i, t;
    unsigned char data;
    int delay = 100000; /* 100 thousand microseconds: 1 tenth of a second */

    while ((t = getopt (argc, argv, "d:")) != -1) {
        switch (t) {
        case 'd':
            delay = atoi (optarg);
            break;
        }
    }

    octopus_init(&octopusc);

    if (octopus_usb_open(&octopusc, 0x0403, 0x6001) < 0)
        octopus_fatal (&octopusc, "Can't open octopus device");

    if (octopus_enable_bitbang(&octopusc, 0xFF) < 0)
        octopus_fatal (&octopusc, "Can't enable bitbang");

    for (i=optind; i < argc ; i++) {
        sscanf (argv[i], "%x", &t);
        data = t;
        if(octopus_write_data(&octopusc, &data, 1) < 0) {
            fprintf(stderr,"write failed for 0x%x: %s\n",
                    data, octopus_get_error_string(&octopusc));
        }
        usleep(delay);
    }

    octopus_usb_close(&octopusc);
    octopus_deinit(&octopusc);
  #endif
    exit (0);
}
